package managedbean.itinerary;

import exception.ExistException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.ItinerarySessionBean;
import session.stateless.SystemMessagesBean;
import session.stateless.TouristAccountSessionBean;
import util.entityhelper.TouristAccountState;
import util.entityhelper.TripItineraryState;
import util.generator.TouristMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class ItineraryViewAllTripItinerariesManagedBean {
    @EJB
    private TouristAccountSessionBean touristAccountMgr;
    @EJB
    private ItinerarySessionBean itineraryMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private TouristAccountState touristAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    
    // fields passed from session
    private Long touristAccountId;
    private String preferredLocale;
    private String itineraryType;
    
    // other fields from database (+ display)
    private List<TripItineraryState> tripItinerariesList;
    
    // selected records
    private TripItineraryState selectedTripItinerary;
    
    // display only
    private MenuModel touristMainMenuModel;
    private MenuModel touristNavMenuModel;

    /** Creates a new instance of ItineraryViewAllItinerariesManagedBean */
    public ItineraryViewAllTripItinerariesManagedBean() {
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        redirect = new PageRedirectorBean();
        /* pre-load values from session and database */
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            touristAccountId = (Long) ctx.getExternalContext().getSessionMap().get("touristAccountId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountId);
            
            if (ctx.getExternalContext().getSessionMap().containsKey("itineraryType")) {
                itineraryType = (String) ctx.getExternalContext().getSessionMap().get("itineraryType");
                ctx.getExternalContext().getSessionMap().put("itineraryType", itineraryType);
            }
            
        }
        else {
            // redirect back to login
            redirect.goTouristLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */
    }
    
    public void initView(PhaseEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        try {
            itineraryMgr.setSelectedLocale(preferredLocale);
            
            if (tripItinerariesList == null) {
                this.tripItinerariesList = new ArrayList<TripItineraryState>();
                try {
                    tripItinerariesList = itineraryMgr.getTripItinerariesReturnByTripItineraryState();
                }
                catch (ExistException ex) {
//                    System.out.println(ex.getMessage());
//                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
                }
                
                for (TripItineraryState t : tripItinerariesList) {
                    String localizedArrivalDate = localization.getLocalizedDateFormat(t.getArrivalDate());
                    String localizedDepartureDate = localization.getLocalizedDateFormat(t.getDepartureDate());
                    System.out.println("localizedArrivalDate: " + localizedArrivalDate);
                    System.out.println("localizedDepartureDate: " + localizedDepartureDate);
                   
                    t.setLocalizedArrivalDate(localizedArrivalDate);
                    t.setLocalizedDepartureDate(localizedDepartureDate);
                }
            }
            
            // main menu 
            TouristMenuGenerator menuGeneratorMain = new TouristMenuGenerator();
            touristMainMenuModel = menuGeneratorMain.generateTouristMainMenu();
            // nav menu
            TouristMenuGenerator menuGeneratorNav = new TouristMenuGenerator();
            touristNavMenuModel = menuGeneratorNav.generateTouristNavMenu();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void viewTripItineraryView(TripItineraryState tripItinerary) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedTripItineraryId", tripItinerary.getId());
//        redirect.goItineraryEditTripItineraryPage();
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/itinerary/ItineraryViewTripItinerary.xhtml"));
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        
        //TODO: edit trip page

    }

    public void viewTripItineraryDelete(TripItineraryState tripItinerary) throws IOException {
        //TODO: delete trip page
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedTripItineraryId", tripItinerary.getId());
//        redirect.goDeleteTripItineraryPage();
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public ItinerarySessionBean getItineraryMgr() {
        return itineraryMgr;
    }

    public void setItineraryMgr(ItinerarySessionBean itineraryMgr) {
        this.itineraryMgr = itineraryMgr;
    }

    public String getItineraryType() {
        return itineraryType;
    }

    public void setItineraryType(String itineraryType) {
        this.itineraryType = itineraryType;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public Long getTouristAccountId() {
        return touristAccountId;
    }

    public void setTouristAccountId(Long touristAccountId) {
        this.touristAccountId = touristAccountId;
    }

    public TouristAccountSessionBean getTouristAccountMgr() {
        return touristAccountMgr;
    }

    public void setTouristAccountMgr(TouristAccountSessionBean touristAccountMgr) {
        this.touristAccountMgr = touristAccountMgr;
    }

    public TouristAccountState getTouristAcctState() {
        return touristAcctState;
    }

    public void setTouristAcctState(TouristAccountState touristAcctState) {
        this.touristAcctState = touristAcctState;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }

    public MenuModel getTouristNavMenuModel() {
        return touristNavMenuModel;
    }

    public void setTouristNavMenuModel(MenuModel touristNavMenuModel) {
        this.touristNavMenuModel = touristNavMenuModel;
    }

    public List<TripItineraryState> getTripItinerariesList() {
        return tripItinerariesList;
    }

    public void setTripItinerariesList(List<TripItineraryState> tripItinerariesList) {
        this.tripItinerariesList = tripItinerariesList;
    }

    public TripItineraryState getSelectedTripItinerary() {
        return selectedTripItinerary;
    }

    public void setSelectedTripItinerary(TripItineraryState selectedTripItinerary) {
        this.selectedTripItinerary = selectedTripItinerary;
    }
}
